//
//  GraphView.m
//  SHHCPhone
//
//  Created by gfsh on 14-6-25.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "GraphView.h"
#import <CoreText/CoreText.h>

@implementation CurveLine



@end

@implementation GraphView
{
    CGPoint bp;
    CGPoint contentScroll;
    
    CGFloat maxWidth;
    CGFloat maxHeight;
    UILabel *titleLab;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = GET_COLOR(@"f2f2f2");
        contentScroll = CGPointZero;
        bp = CGPointZero;
        _frontLineIndex = -1;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
        [self addGestureRecognizer:pan];
        
        titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 30)];
        titleLab.backgroundColor = GET_COLOR(@"f2f2f2");
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.font = [UIFont systemFontOfSize:14];
        [self addSubview:titleLab];
    }
    return self;
}

- (void)setTitle:(NSString *)title
{
    titleLab.text = title;
}

- (void)setTitleColor:(UIColor *)titleColor
{
    titleLab.textColor = titleColor;
}

- (void)setData:(NSArray *)data
{
    _data = data;
    NSMutableArray *newData = [NSMutableArray array];
    if (self.frontLineIndex>-1) {
        for (CurveLine *cl in self.data) {
            NSInteger index = [self.data indexOfObject:cl];
            if (index != self.frontLineIndex) {
                [newData addObject:cl];
            }
        }
        [newData addObject:self.data[self.frontLineIndex]];
        _data = newData;
    }
    contentScroll = CGPointMake(0, 0);
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context=UIGraphicsGetCurrentContext();
//    CGContextTranslateCTM(context, 0.0f, self.bounds.size.height);
//    CGContextScaleCTM(context, 1, -1);
    
    maxWidth = (self.xAsis.count-1)*self.cellWidth;
    maxHeight = (self.yAsis.count-1)*self.cellHeight;
    UIFont *font = [UIFont systemFontOfSize:10];
    
    CGFloat yStrMaxWidth = [self.yAsis[self.yAsis.count-1] sizeWithFont:font forWidth:100.f lineBreakMode:NSLineBreakByWordWrapping].width+3;

    float vH = 0;
    for (int i=0; i<self.yAsis.count; i++) {
        int height = self.bounds.size.height-30 - self.cellHeight * i;
        vH = height + contentScroll.y;
        vH = vH<30?30:(vH>self.bounds.size.height-30?self.bounds.size.height-30:vH);

        [GET_COLOR(@"dddddd") set];
        CGContextMoveToPoint(context, yStrMaxWidth, vH);
        CGContextAddLineToPoint(context, self.bounds.size.width-1, vH);
        CGContextStrokePath(context);
        NSString *yStr = self.yAsis[i];
        [GET_COLOR(@"f2f2f2") set];
        CGContextFillRect(context, CGRectMake(0, vH-10, yStrMaxWidth-5, 10));
        [GET_COLOR(@"3e3e3e") set];
        [yStr drawAtPoint:CGPointMake(0, vH-10) withFont:font];

    }
    
    float vW = 0;
    for (int i=0; i<self.xAsis.count; i++) {
        int width = self.cellWidth * i+yStrMaxWidth;
        vW = width + contentScroll.x;
        vW = vW<yStrMaxWidth?yStrMaxWidth:(vW);
        [GET_COLOR(@"dddddd") set];
        CGContextMoveToPoint(context, vW, self.bounds.size.height-30);
        CGContextAddLineToPoint(context, vW, vH);
        CGContextStrokePath(context);
        NSString *xStr = self.xAsis[i];
        [GET_COLOR(@"f2f2f2") set];
        CGContextFillRect(context, CGRectMake(vW, self.bounds.size.height-30, self.cellWidth, 30));
        [GET_COLOR(@"3e3e3e") set];
        [xStr drawAtPoint:CGPointMake(vW, self.bounds.size.height-18) withFont:font];
    }
    
    for (CurveLine *cl in self.data) {
        NSArray *points = cl.points;
        CGPoint tempP = CGPointZero;
        [cl.lineColor set];
        for (NSString *point in points) {
            CGPoint p = CGPointFromString(point);
            CGContextSetLineWidth(context, 2);
            float x = p.x*self.cellWidth+yStrMaxWidth+contentScroll.x;
            float y = -p.y*self.cellHeight+self.bounds.size.height-30+contentScroll.y;
            float tx = tempP.x*self.cellWidth+yStrMaxWidth+contentScroll.x;
            float ty = -tempP.y*self.cellHeight+self.bounds.size.height-30+contentScroll.y;
            BOOL flag = NO;
            BOOL flag1 = NO;

            if (!CGPointEqualToPoint(tempP, CGPointZero)) {
                if ((tx>=yStrMaxWidth&&ty<=self.bounds.size.height-30&&ty>=30)) {
                    
                }else{
                    flag = YES;
                    if (tx<yStrMaxWidth&&x-tx>0) {
                        ty = (30-tx)*(y-ty)/(x-tx)+ty;
                        tx = yStrMaxWidth;
                    }
                    if (ty>self.bounds.size.height-30&&ty-y>0) {
                        tx = (ty-(self.bounds.size.height-30))*(x-tx)/(ty-y)+tx;
                        ty = self.bounds.size.height-30;
                    }
                }
                CGContextMoveToPoint(context, tx,ty);
                if ((x>=yStrMaxWidth&&y<=self.bounds.size.height-30&&y>=30)) {
                    
                }else{
                    flag1 = YES;
                    if (x<yStrMaxWidth&&tx-x>0) {
                        y = (30-x)*(ty-y)/(tx-x)+y;
                        x = yStrMaxWidth;
                    }
                    if (y>self.bounds.size.height-30&&y-ty>0) {
                        x = x-(y-(self.bounds.size.height-30))*(x-tx)/(y-ty);
                        y = self.bounds.size.height-30;
                    }
                }
                if (!(flag&&flag1)) {
                    CGContextAddLineToPoint(context, x, y);
                    CGContextStrokePath(context);
                }
                if (!((x==yStrMaxWidth||y==self.bounds.size.height-30)&&(flag||flag1))) {
                    if (x>=yStrMaxWidth&&y<=self.bounds.size.height-30&&y>=30) {
                    CGContextFillEllipseInRect(context, CGRectMake(x-3, y-3, 6, 6));
                    CGContextStrokePath(context);
                    }
                }
            }else{
                if (x>=yStrMaxWidth&&y<=self.bounds.size.height-30&&y>=30) {
                    CGContextFillEllipseInRect(context, CGRectMake(x-3, y-3, 6, 6));
                    CGContextStrokePath(context);
                }
            }
            tempP = p;
        }
    }

}

- (void)pan:(UIGestureRecognizer*)pangr
{
    switch (pangr.state) {
        case UIGestureRecognizerStateBegan:
        {
            bp = [pangr locationInView:self];
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            CGPoint p = [pangr locationInView:self];
            contentScroll.x+=p.x-bp.x;
            contentScroll.y+=p.y-bp.y;
            if (contentScroll.x >0||maxWidth<=self.bounds.size.width-30) {
                contentScroll.x=0;
            }
            if(contentScroll.y<0||maxHeight<=self.bounds.size.height-30-30)
            {
                contentScroll.y=0;
            }
//            if (maxHeight>self.bounds.size.height-30-30 && maxHeight+contentScroll.y>=60) {
//                contentScroll.y -= p.y-bp.y;
//            }
            if (maxWidth>self.bounds.size.width-30 && maxWidth+contentScroll.x<=self.bounds.size.width-30-20) {
                contentScroll.x -= p.x-bp.x;
            }
            [self setNeedsDisplay];
            bp = p;
        }
            break;
        default:
            break;
    }
}


@end
